package net.inxweb.bluemeerkat.domain.infrastructure.persistence.hibernate;

import java.util.List;

import javax.annotation.Resource;

import net.inxweb.bluemeerkat.domain.model.glossary.GlossaryInstance;
import net.inxweb.bluemeerkat.domain.repository.GlossaryInstanceRepository;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

@Repository("glossaryInstanceRepositoryHibernate")
public class GlossaryInstanceRepositoryHibernate implements GlossaryInstanceRepository {
	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;

	public Long add(GlossaryInstance glossaryInstance) {
		return (Long)sessionFactory.getCurrentSession().save(glossaryInstance);
	}

	public GlossaryInstance find(Long id) { 
		return (GlossaryInstance)sessionFactory.getCurrentSession().get(GlossaryInstance.class, id);
	}

	@SuppressWarnings("unchecked")
	public List<GlossaryInstance> findAll() {
		return (List<GlossaryInstance>)sessionFactory.getCurrentSession().createQuery("from net.inxweb.bluemeerkat.domain.model.glossary.GlossaryInstance");
	}

	public void modify(GlossaryInstance glossaryInstance) {
		sessionFactory.getCurrentSession().update(glossaryInstance);
		
	}

	public void remove(Long id) { 
		GlossaryInstance glossaryInstance = (GlossaryInstance)sessionFactory.getCurrentSession().get(GlossaryInstance.class, id);
		this.remove(glossaryInstance);
	}

	public void remove(GlossaryInstance glossaryInstance) {
		sessionFactory.getCurrentSession().delete(glossaryInstance); 
	} 
}
